1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gdk.Texture;
26 
27 private import gdk.PaintableIF;
28 private import gdk.PaintableT;
29 private import gdk.c.functions;
30 public  import gdk.c.types;
31 private import gdkpixbuf.Pixbuf;
32 private import gio.FileIF;
33 private import gio.IconIF;
34 private import gio.IconT;
35 private import gio.LoadableIconIF;
36 private import gio.LoadableIconT;
37 private import glib.Bytes;
38 private import glib.ConstructionException;
39 private import glib.ErrorG;
40 private import glib.GException;
41 private import glib.Str;
42 private import gobject.ObjectG;
43 
44 
45 /**
46  * `GdkTexture` is the basic element used to refer to pixel data.
47  * 
48  * It is primarily meant for pixel data that will not change over
49  * multiple frames, and will be used for a long time.
50  * 
51  * There are various ways to create `GdkTexture` objects from a
52  * [class@GdkPixbuf.Pixbuf], or a Cairo surface, or other pixel data.
53  * 
54  * The ownership of the pixel data is transferred to the `GdkTexture`
55  * instance; you can only make a copy of it, via [method@Gdk.Texture.download].
56  * 
57  * `GdkTexture` is an immutable object: That means you cannot change
58  * anything about it other than increasing the reference count via
59  * [method@GObject.Object.ref], and consequently, it is a thread-safe object.
60  */
61 public class Texture : ObjectG, PaintableIF, IconIF, LoadableIconIF
62 {
63 	/** the main Gtk struct */
64 	protected GdkTexture* gdkTexture;
65 
66 	/** Get the main Gtk struct */
67 	public GdkTexture* getTextureStruct(bool transferOwnership = false)
68 	{
69 		if (transferOwnership)
70 			ownedRef = false;
71 		return gdkTexture;
72 	}
73 
74 	/** the main Gtk struct as a void* */
75 	protected override void* getStruct()
76 	{
77 		return cast(void*)gdkTexture;
78 	}
79 
80 	/**
81 	 * Sets our main struct and passes it to the parent class.
82 	 */
83 	public this (GdkTexture* gdkTexture, bool ownedRef = false)
84 	{
85 		this.gdkTexture = gdkTexture;
86 		super(cast(GObject*)gdkTexture, ownedRef);
87 	}
88 
89 	// add the Paintable capabilities
90 	mixin PaintableT!(GdkTexture);
91 
92 	// add the Icon capabilities
93 	mixin IconT!(GdkTexture);
94 
95 	// add the LoadableIcon capabilities
96 	mixin LoadableIconT!(GdkTexture);
97 
98 	/**
99 	 * Creates a new texture by loading an image from a file or a resource.
100 	 *
101 	 * The file format is detected automatically. The supported formats
102 	 * are PNG and JPEG, though more formats might be available.
103 	 *
104 	 * If %NULL is returned, then @error will be set.
105 	 *
106 	 * This function is threadsafe, so that you can e.g. use GTask
107 	 * and g_task_run_in_thread() to avoid blocking the main thread
108 	 * while loading a big image.
109 	 *
110 	 * Params:
111 	 *     path = the filename or resourcename to load
112 	 *     resource = true if load from resource
113 	 *
114 	 * Returns: A newly-created `GdkTexture`
115 	 *
116 	 * Since: 4.6
117 	 *
118 	 * Throws: GException on failure.
119 	 * Throws: ConstructionException GTK+ fails to create the object.
120 	 */
121 	public this(string path, bool resource = false)
122 	{
123 		GError* err = null; GdkTexture *__p;
124 
125 		if (resource) __p = gdk_texture_new_from_resource(Str.toStringz(path));
126 		else __p = gdk_texture_new_from_filename(Str.toStringz(path), &err);
127 
128 		if (err !is null)
129 		{
130 			throw new GException( new ErrorG(err) );
131 		}
132 
133 		if(__p is null)
134 		{
135 			throw new ConstructionException("null returned by new_from_" ~ (resource ? "resource" : "filename"));
136 		}
137 
138 		this(cast(GdkTexture*) __p, true);
139 	}
140 
141 	/**
142 	 */
143 
144 	/** */
145 	public static GType getType()
146 	{
147 		return gdk_texture_get_type();
148 	}
149 
150 	/**
151 	 * Creates a new texture object representing the `GdkPixbuf`.
152 	 *
153 	 * This function is threadsafe, so that you can e.g. use GTask
154 	 * and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
155 	 * while loading a big image.
156 	 *
157 	 * Params:
158 	 *     pixbuf = a `GdkPixbuf`
159 	 *
160 	 * Returns: a new `GdkTexture`
161 	 *
162 	 * Throws: ConstructionException GTK+ fails to create the object.
163 	 */
164 	public this(Pixbuf pixbuf)
165 	{
166 		auto __p = gdk_texture_new_for_pixbuf((pixbuf is null) ? null : pixbuf.getPixbufStruct());
167 
168 		if(__p is null)
169 		{
170 			throw new ConstructionException("null returned by new_for_pixbuf");
171 		}
172 
173 		this(cast(GdkTexture*) __p, true);
174 	}
175 
176 	/**
177 	 * Creates a new texture by loading an image from memory,
178 	 *
179 	 * The file format is detected automatically. The supported formats
180 	 * are PNG and JPEG, though more formats might be available.
181 	 *
182 	 * If %NULL is returned, then @error will be set.
183 	 *
184 	 * This function is threadsafe, so that you can e.g. use GTask
185 	 * and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
186 	 * while loading a big image.
187 	 *
188 	 * Params:
189 	 *     bytes = a `GBytes` containing the data to load
190 	 *
191 	 * Returns: A newly-created `GdkTexture`
192 	 *
193 	 * Since: 4.6
194 	 *
195 	 * Throws: GException on failure.
196 	 * Throws: ConstructionException GTK+ fails to create the object.
197 	 */
198 	public this(Bytes bytes)
199 	{
200 		GError* err = null;
201 
202 		auto __p = gdk_texture_new_from_bytes((bytes is null) ? null : bytes.getBytesStruct(), &err);
203 
204 		if (err !is null)
205 		{
206 			throw new GException( new ErrorG(err) );
207 		}
208 
209 		if(__p is null)
210 		{
211 			throw new ConstructionException("null returned by new_from_bytes");
212 		}
213 
214 		this(cast(GdkTexture*) __p, true);
215 	}
216 
217 	/**
218 	 * Creates a new texture by loading an image from a file.
219 	 *
220 	 * The file format is detected automatically. The supported formats
221 	 * are PNG and JPEG, though more formats might be available.
222 	 *
223 	 * If %NULL is returned, then @error will be set.
224 	 *
225 	 * This function is threadsafe, so that you can e.g. use GTask
226 	 * and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
227 	 * while loading a big image.
228 	 *
229 	 * Params:
230 	 *     file = `GFile` to load
231 	 *
232 	 * Returns: A newly-created `GdkTexture`
233 	 *
234 	 * Throws: GException on failure.
235 	 * Throws: ConstructionException GTK+ fails to create the object.
236 	 */
237 	public this(FileIF file)
238 	{
239 		GError* err = null;
240 
241 		auto __p = gdk_texture_new_from_file((file is null) ? null : file.getFileStruct(), &err);
242 
243 		if (err !is null)
244 		{
245 			throw new GException( new ErrorG(err) );
246 		}
247 
248 		if(__p is null)
249 		{
250 			throw new ConstructionException("null returned by new_from_file");
251 		}
252 
253 		this(cast(GdkTexture*) __p, true);
254 	}
255 
256 	/**
257 	 * Downloads the @texture into local memory.
258 	 *
259 	 * This may be an expensive operation, as the actual texture data
260 	 * may reside on a GPU or on a remote display server.
261 	 *
262 	 * The data format of the downloaded data is equivalent to
263 	 * %CAIRO_FORMAT_ARGB32, so every downloaded pixel requires
264 	 * 4 bytes of memory.
265 	 *
266 	 * Downloading a texture into a Cairo image surface:
267 	 * ```c
268 	 * surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
269 	 * gdk_texture_get_width (texture),
270 	 * gdk_texture_get_height (texture));
271 	 * gdk_texture_download (texture,
272 	 * cairo_image_surface_get_data (surface),
273 	 * cairo_image_surface_get_stride (surface));
274 	 * cairo_surface_mark_dirty (surface);
275 	 * ```
276 	 *
277 	 * Params:
278 	 *     data = pointer to enough memory to be filled with the
279 	 *         downloaded data of @texture
280 	 *     stride = rowstride in bytes
281 	 */
282 	public void download(char[] data, size_t stride)
283 	{
284 		gdk_texture_download(gdkTexture, data.ptr, stride);
285 	}
286 
287 	/**
288 	 * Returns the height of the @texture, in pixels.
289 	 *
290 	 * Returns: the height of the `GdkTexture`
291 	 */
292 	public int getHeight()
293 	{
294 		return gdk_texture_get_height(gdkTexture);
295 	}
296 
297 	/**
298 	 * Returns the width of @texture, in pixels.
299 	 *
300 	 * Returns: the width of the `GdkTexture`
301 	 */
302 	public int getWidth()
303 	{
304 		return gdk_texture_get_width(gdkTexture);
305 	}
306 
307 	/**
308 	 * Store the given @texture to the @filename as a PNG file.
309 	 *
310 	 * This is a utility function intended for debugging and testing.
311 	 * If you want more control over formats, proper error handling or
312 	 * want to store to a [iface@Gio.File] or other location, you might want to
313 	 * use [method@Gdk.Texture.save_to_png_bytes] or look into the
314 	 * gdk-pixbuf library.
315 	 *
316 	 * Params:
317 	 *     filename = the filename to store to
318 	 *
319 	 * Returns: %TRUE if saving succeeded, %FALSE on failure.
320 	 */
321 	public bool saveToPng(string filename)
322 	{
323 		return gdk_texture_save_to_png(gdkTexture, Str.toStringz(filename)) != 0;
324 	}
325 
326 	/**
327 	 * Store the given @texture in memory as a PNG file.
328 	 *
329 	 * Use [ctor@Gdk.Texture.new_from_bytes] to read it back.
330 	 *
331 	 * If you want to serialize a texture, this is a convenient and
332 	 * portable way to do that.
333 	 *
334 	 * If you need more control over the generated image, such as
335 	 * attaching metadata, you should look into an image handling
336 	 * library such as the gdk-pixbuf library.
337 	 *
338 	 * If you are dealing with high dynamic range float data, you
339 	 * might also want to consider [method@Gdk.Texture.save_to_tiff_bytes]
340 	 * instead.
341 	 *
342 	 * Returns: a newly allocated `GBytes` containing PNG data
343 	 *
344 	 * Since: 4.6
345 	 */
346 	public Bytes saveToPngBytes()
347 	{
348 		auto __p = gdk_texture_save_to_png_bytes(gdkTexture);
349 
350 		if(__p is null)
351 		{
352 			return null;
353 		}
354 
355 		return new Bytes(cast(GBytes*) __p, true);
356 	}
357 
358 	/**
359 	 * Store the given @texture to the @filename as a TIFF file.
360 	 *
361 	 * GTK will attempt to store data without loss.
362 	 *
363 	 * Params:
364 	 *     filename = the filename to store to
365 	 *
366 	 * Returns: %TRUE if saving succeeded, %FALSE on failure.
367 	 *
368 	 * Since: 4.6
369 	 */
370 	public bool saveToTiff(string filename)
371 	{
372 		return gdk_texture_save_to_tiff(gdkTexture, Str.toStringz(filename)) != 0;
373 	}
374 
375 	/**
376 	 * Store the given @texture in memory as a TIFF file.
377 	 *
378 	 * Use [ctor@Gdk.Texture.new_from_bytes] to read it back.
379 	 *
380 	 * This function is intended to store a representation of the
381 	 * texture's data that is as accurate as possible. This is
382 	 * particularly relevant when working with high dynamic range
383 	 * images and floating-point texture data.
384 	 *
385 	 * If that is not your concern and you are interested in a
386 	 * smaller size and a more portable format, you might want to
387 	 * use [method@Gdk.Texture.save_to_png_bytes].
388 	 *
389 	 * Returns: a newly allocated `GBytes` containing TIFF data
390 	 *
391 	 * Since: 4.6
392 	 */
393 	public Bytes saveToTiffBytes()
394 	{
395 		auto __p = gdk_texture_save_to_tiff_bytes(gdkTexture);
396 
397 		if(__p is null)
398 		{
399 			return null;
400 		}
401 
402 		return new Bytes(cast(GBytes*) __p, true);
403 	}
404 }